package com.qigao.mall.web.controller.app.user;

import com.qigao.mall.api.RefundService;
import com.qigao.mall.commons.dto.RefundApplyDto;
import com.qigao.mall.commons.dto.RefundOrderResultDto;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.app.RefundApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/user/refund")
@Api(tags = "App退款相关API",value = "App退款相关API")
public class AppRefundController {
    private static Logger logger= LoggerFactory.getLogger(AppRefundController.class);
    @Resource
    RefundService refundService;

    //1.退款申请
    @ApiOperation("退换货申请")
    @PostMapping("/apply")
    public Result applyRefund(@RequestBody RefundApplyVo  refundApplyVo, HttpServletRequest request){
        try{
            ValidatorUtil.validateEntity(refundApplyVo);
            RefundApplyDto refundApplyDto=new RefundApplyDto();
            BeanUtils.copyProperties(refundApplyVo,refundApplyDto);
            refundApplyDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return refundService.applyRefund(refundApplyDto);
        }catch (BusinessException be){
            logger.error("退换货申请异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("退换货申请发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.按照订单号查询退款
    @GetMapping("/findRefundByOrderNo/{orderNo}")
    public Result<RefundOrderResultDto> findRefundByOrderNo(@PathVariable("orderNo")String orderNo, HttpServletRequest request){
        try{
            return refundService.findRefundByOrderNo(orderNo);
        }catch (BusinessException be){
            logger.error("按照订单号查询退款业务异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("按照订单号查询退款发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }


}
